﻿using AutoMapper;
using Blog.Repository;
using XjjXmm.Infrastructure.Repository;
using XjjXmm.Infrastructure.User;

namespace Blog.Service
{
    public class CrudService<TModel, TEntity> : ICrudService<TModel> where TEntity : class, new()
    {
        private readonly IRepositoryBase<TEntity> _repositoryBase;
        private readonly IUser _user;

        /// <summary>
        /// 映射
        /// </summary>
        public IMapper _mapper;
        public CrudService(IRepositoryBase<TEntity> repositoryBase, IUser user, IMapper mapper)
        {
            _repositoryBase = repositoryBase;
            _user = user;
            _mapper = mapper;
        }

        public async Task<bool> Add(TModel model)
        {
            var entity = _mapper.Map<TEntity>(model);
            FillAdd(entity);
            return await _repositoryBase.Add(entity);
        }

        public async Task<bool> Add(List<TModel> model)
        {
            var entity = _mapper.Map<List<TEntity>>(model);
            FillAdd(entity);
            return await _repositoryBase.Add(entity);
        }

        public async Task<bool> Delete(dynamic id)
        {
            return await _repositoryBase.Delete(id);
        }

        public async Task<TModel> Find(dynamic id)
        {
            return await _repositoryBase.Find(id);
        }

        public async Task<List<TModel>> GetAll()
        {
            var entities = await _repositoryBase.GetAll();

            return _mapper.Map<List<TModel>>(entities);
        }

        public async Task<bool> SoftDelete(dynamic id)
        {
            return await _repositoryBase.Delete(id);
        }

        public async Task<bool> SoftDelete(dynamic[] id)
        {
            return await _repositoryBase.Delete(id);
        }

        public async Task<bool> Update(TModel model)
        {
            var entity = _mapper.Map<TEntity>(model);
            FillUpdate(entity);
            return await _repositoryBase.Update(entity);
        }

        private void FillAdd(TEntity entity)
        {
            if (entity is EntityFull)
            {
                var tmp = entity as EntityFull;
                tmp.CreatedTime = DateTime.Now;
                tmp.CreatedUserName = _user.NickName;
                tmp.CreatedUserId = _user.Id;

                tmp.ModifiedTime = DateTime.Now;
                tmp.ModifiedUserId = _user.Id;
                tmp.ModifiedUserName = _user.NickName;
            }
            else if (entity is EntityAdd)
            {
                var tmp = entity as EntityAdd;
                tmp.CreatedTime = DateTime.Now;
                tmp.CreatedUserName = _user.NickName;
                tmp.CreatedUserId = _user.Id;
            }
        }

        private void FillAdd(List<TEntity> entities)
        {
            entities.ForEach(e => FillAdd(e));
        }
        private void FillUpdate(TEntity entity)
        {
            if (entity is EntityFull)
            {
                var tmp = entity as EntityFull;

                tmp.ModifiedTime = DateTime.Now;
                tmp.ModifiedUserId = _user.Id;
                tmp.ModifiedUserName = _user.NickName;
            }
        }

        private void FillUpdate(List<TEntity> entities)
        {
            entities.ForEach(e => FillUpdate(e));
        }
    }
}